package com.passioneffect.adapter {
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;

	import com.passioneffect.collection.Collection;
	import com.passioneffect.collection.Iterator;
	import com.passioneffect.display.Component;	

	/**
	 * @author Passion Effect
	 */
	public class ContentMovieClipAdapter {

		private var mc : MovieClip;
		private var _data : Object;
		private var index : int;
		private var component : Component;
		private var x : Number = 0;
		private var y : Number = 0;
		public var id : String;

		public function ContentMovieClipAdapter() {
		}
		
		public function init(index : int,data : Object,c : Component) : void {
			this.component = c;
			this.index = index;
			data.index = index;
			this._data = data;
			id = data.id;
		}
		
		public function get mcClassName():String{
			
			return component.contentClass;
		}
		
		public function takeOverBy(component : Component) : void {
			
			this.component = component;
			if(mc)mc.changeComponent();
		}

		public function get data() : Object {
			return _data;
		}

		public function getMovieClip(initObj : Object = null) : MovieClip {
			var className:String=mcClassName;
			
			if(mc == null) {
				if(className==null){
					className=data.clip;
				}
				var def : Object = getDefinitionByName(className);
				mc = new def();
				
				if(initObj)
					for(var i:String in initObj) {
					mc[i] = initObj[i];
				}
				
				mc.init(this);
				mc.onContentData(_data);
			}
			
			return mc;
		}
		
		public function getPercentMouseX():Number{
			return mc.getPercentMouseX();
		}
		
		public function getComponent() : Component {
			return component;
		}

		public function removeMovieClip() : MovieClip {
			if(mc == null)return null;
			var buff : MovieClip = mc;
			
			if(mc.autoRemove && mc.parent && !mc.selected) {
				mc.parent.removeChild(mc);
				mc = null;
			}else {
				mc.visible = false;
			}
			
			return buff;
		}

		public function getIndex() : int {
			return index;
		}

		public function setIndex(index : int) : void {
			this.index = index;
		}

		public function getRect() : Rectangle {
			return new Rectangle(mc.x, mc.y, mc.width, mc.height);
		}
		
		public function getBounds():Object{
			var c:Object=getLayoutValue();
			if(c==null){
				c=component.unselectedSize;
			}
			return new Rectangle(mc.x,mc.y,c.width,c.height);
		}
		
		public function getLayoutValue():Object{
			return component.getLayoutValue(this);
		}
		
		public function setMovieClip(mc : MovieClip) : void {
			if(this.mc){
				if(this.mc.parent){
					this.mc.parent.removeChild(this.mc);
				}
			}
			this.mc = mc;
		}
	}
}
